package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.dao.CategoryService;
import com.ktjiaoyu.ktmall.entity.Category;

import java.util.ArrayList;
import java.util.List;

public class CategoryServiceimpl extends BaseDao implements CategoryService {
    private final CategoryDao categoryDao=new CategourDaoimpl();

    @Override
    public List<Category> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public List<Category> findTree() {
        List<Category> all=categoryDao.findAll();
        return buildTree(all,"0");
    }
    private  List<Category> buildTree(List<Category> all,String parentid){
        List<Category> tree=new ArrayList<Category>();
        for (Category cat : all) {
            if(cat.getParentId()!=null&&cat.getParentId().equals(parentid)){
                List<Category> children=buildTree(all,cat.getId());
                cat.setChildren(children);
                tree.add(cat);
            }
        }
        return tree;
    }
}
